//
//  FPForgetPasswordController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/4/10.
//

#import "FPForgetPasswordController.h"
#import "FPSetPasswordController.h"

@interface FPForgetPasswordController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconToTop;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgToTop;

@property (weak, nonatomic) IBOutlet UILabel *tipLab;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property (nonatomic , strong) NSString *paramPhone;
@property (nonatomic , strong) NSString *paramCode;

@end

@implementation FPForgetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initUI {
    
    [self setTitle:@"修改密码" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.bgToTop.constant = UIDevice.navigationFullHeight;
    self.iconToTop.constant = UIDevice.navigationFullHeight + 20.f;
    
    self.backView.clipsToBounds = YES;
    self.backView.layer.cornerRadius = 8.f;
    
    self.nextBtn.clipsToBounds = YES;
    self.nextBtn.layer.cornerRadius = 22.f;
    self.nextBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    self.lineView.backgroundColor = [[UIColor hexColor:@"#CCCCCC"] colorWithAlphaComponent:.5f];
    
    self.codeTextField.delegate = self;
    self.phoneTextField.delegate = self;
    
    [self.codeBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
       
        [self.view endEditing:YES];
        
        if (self.paramPhone.length < 11) {
            
            [self.phoneTextField shake];
            return;
        }
        else {
            
            [self startCountDownTimer];
        }
    }];
    
    [self.nextBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
        
        [self.view endEditing:YES];
        
        // 校验验证码
        // ...
        FPSetPasswordController *vc = [[FPSetPasswordController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
}



- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if ([textField isEqual:self.phoneTextField]) {
        
        self.paramPhone = textField.text;
    }
    else if ([textField isEqual:self.codeTextField]) {
        
        self.paramCode = textField.text;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if ([string isEqualToString:@""]) {
        
        return YES;
    }
    
    // 数字 检查字符串是否为数字
    NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    
    if ([string rangeOfCharacterFromSet:nonDigitCharacterSet].location != NSNotFound) {
        
        return NO;  // 如果输入的字符不是数字，则不允许输入
    }
    
    if ([textField isEqual:self.phoneTextField]) {
        
        return textField.text.length < 11;
    }
    else if ([textField isEqual:self.codeTextField]) {
    
        return textField.text.length < 4;
    }
    
    return YES;
}

#pragma mark - 开始倒计时 验证码
- (void)startCountDownTimer {
    
    [self.view endEditing:YES];
    
    NSDate *oldDate = [NSDate date];
    // 倒计时时间
    __block NSInteger timeOut = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        // 倒计时结束
        if (timeOut <= 0) {
            
            // 再次获取
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
                self.codeBtn.userInteractionEnabled = YES;
                self.codeBtn.enabled = YES;
                [self.codeBtn setTitleColor:MAIN_BLUE_COLOR forState:(UIControlStateNormal)];
            });
        }
        // 正在倒计时
        else {
            
            NSDate *newDate = [NSDate date];
            NSTimeInterval timeInterva = [newDate timeIntervalSinceDate:oldDate];
            int seconds2 = (60 -timeInterva);
            NSString *timeStr = [NSString stringWithFormat:@"%d",seconds2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeBtn setTitle:[NSString stringWithFormat:@"重新获取(%@)", timeStr] forState:UIControlStateNormal];
                self.codeBtn.userInteractionEnabled = NO;
                self.codeBtn.enabled = NO;
                [self.codeBtn setTitleColor:[UIColor hexColor:@"#999999"] forState:(UIControlStateNormal)];
            });
            //bug 解决
            if (seconds2 <= 1) {
                timeOut = 1;
            }
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}



@end
